package com.googlecode.cswish.view;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.components.UIBean;

import com.googlecode.cswish.Config;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.struts.spring.ObjectFactory;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author Samlin.Zhang Date: 2008-6-10
 */
public class PageInfoTag extends UIBean {
	private static final Log logger = LogFactory.getLog(PageInfoTag.class);
	private static final String defaultTemplate = "pageinfotag";

    private ObjectFactory objectFactory;
    private Config config;
    
    @Inject
    public void setObjectFactory(com.opensymphony.xwork2.ObjectFactory objectFactory) {
    	this.objectFactory = ObjectFactory.getInstance();
    	this.config = this.objectFactory.getBean(Config.class, false);
    }
	
	private Object pageInfo;
	private String url;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Object getPageinfo() {
		PageInfo tmpPage = new PageInfo(config.getPageSize());
		if (pageInfo instanceof PageInfo) {
			return pageInfo;
		}

		if (pageInfo instanceof PageInfo) {
			return pageInfo;
		} else if (pageInfo instanceof String) {
			Object page = findValue((String) pageInfo);
			if (page instanceof PageInfo) {
				return (PageInfo) page;
			} else {
				logger.error("the PageInfo is Null !!!");
			}
		}
		return tmpPage;
	}

	public void setPageInfo(Object pageinfo) {
		this.pageInfo = pageinfo;
	}

	public PageInfoTag(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		super(stack, request, response);
	}

	@Override
	protected String getDefaultTemplate() {
		return defaultTemplate;
	}

	@Override
	protected void evaluateExtraParams() {
		if (getPageinfo() != null) {
			addParameter("pageInfo", getPageinfo());
		}
		if (getUrl() != null) {
			addParameter("url", getUrl());
		}
	}

}